﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using FCNS.KeepMoney.Util;
using FCNS.KeepMoneyLibrary.Data;

namespace FCNS.KeepMoney.UI
{
    public partial class GtdViewTask : Control
    {
        public GtdViewTask()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserMouse, true);
            this.SetStyle(ControlStyles.ResizeRedraw, false);
            this.SetStyle(ControlStyles.Opaque, true);

            finish = Properties.Resources.close_16;
            levelNull = Properties.Resources.star_black__16;
            level = Properties.Resources.star_yellow_16;
            note = Properties.Resources.note_16;
            close = Properties.Resources.close_16;
        }

        Bitmap offBmp = null;
        Image finish, levelNull,level, note, close;

        GtdEntryField task = null;
        public GtdEntryField Task
        {
            get { return task; }
            set { task = value; RePaint(); }
        }

        private void GtdViewTask_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(offBmp,0,0);
        }

        private void GtdViewTask_Resize(object sender, EventArgs e)
        {
            if (this.Height < 16)
                this.Height = 16;
            if (this.Width < 200)
                this.Width = 200;

            RePaint();
        }


        private void RePaint()
        {
            if ((this.ClientRectangle.Width < 1) || (this.ClientRectangle.Height < 1))
                return;

            if ((offBmp == null) || (offBmp.Height != this.ClientRectangle.Height) || (offBmp.Width != this.ClientRectangle.Width))
            {
                if (offBmp != null)
                    offBmp.Dispose();

                offBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            }

            Graphics g = PaintTask();
            g.Dispose();
            Paint2();
        }

        private Graphics PaintTask()
        {
            int padLef = 27;
            int padTop = (this.Height - 16) / 2;
            Graphics g = Graphics.FromImage(offBmp);
            g.Clear(Color.White);

            //所有绘制左内向5像素
            g.DrawImage(finish, 5, padTop);//26

            int levelCount = 0;
            string title = string.Empty;
            string endTime = string.Empty;
            if (task != null)
            {
                levelCount = task.Level;
                title = task.Title;
                endTime = task.EndTime.ToString(Program.AppConfig.App.DateFormat);
            }

            for (int i = 0; i < 5; i++)
            {
                if (levelCount != 0 && i < levelCount)
                    g.DrawImage(level, i * 17+padLef, padTop, 16, 16);
                else
                    g.DrawImage(levelNull, i * 17+padLef, padTop, 16, 16);
            }

            g.DrawImage(close, this.Width - 21, padTop);
            g.DrawImage(note, this.Width - 40, padTop);

            padLef = 118;
            int padRight = this.Width - 40 - endTime.Length * 8;
            StringFormat sf=new StringFormat();
            sf.Alignment=StringAlignment.Near;
            if ((this.Width - padLef - 38) > (title.Length + endTime.Length) * this.Font.Size+50 )
            {
                g.DrawString(title, this.Font, Brushes.Black, padLef, padTop, sf);
                g.DrawString(endTime, this.Font, Brushes.Black, padRight, padTop, sf);
            }
            else
            {
                g.DrawString(title,this.Font, Brushes.Black,new RectangleF(padLef,padTop,(int)((padRight-padLef)/this.Font.Size),16));
                g.DrawString(endTime, this.Font, Brushes.Black, padRight, padTop, sf);
            }
            sf.Dispose();
            return g;
        }

        private void Paint2()
        {
            if ((offBmp == null) || (offBmp.Height != this.ClientRectangle.Height) || (offBmp.Width != this.ClientRectangle.Width))
            {
                RePaint();
                return;
            }
            if ((this.ClientRectangle.Width < 1) || (this.ClientRectangle.Height < 1))
                return;

            if (!this.IsDisposed)//取消这里会导致控件无法刷新,不加验证在切换模式的时候会报错
            {
                Graphics drawingSurface = Graphics.FromHwnd(this.Handle);
                drawingSurface.DrawImage(offBmp, 0, 0);
                drawingSurface.Dispose();
            }
        }
    }
}
